package com.chanceit.ssss.activity;

import org.gl.GlobalVariable;
import org.gl.utils.AndroidUtil;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.chanceit.ssss.common.ActivityActionName;

public class SettingsActivity extends BaseFragmentActivity {
	EditText ip_EditText;
	EditText port_EditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		ip_EditText = ((EditText) findViewById(R.id.ip));
		port_EditText = ((EditText) findViewById(R.id.port));
		ip_EditText.setText(AndroidUtil.getServerIPConfig());
		port_EditText.setText(AndroidUtil.getServerPortConfig());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setting_button:

			String ip = ip_EditText.getText().toString();
			String port = port_EditText.getText().toString();
			if (TextUtils.isEmpty(ip)) {
				ip_EditText.setError(getString(R.string.not_null));
				ip_EditText.requestFocus();
			} else if (TextUtils.isEmpty(port)) {
				port_EditText.setError(getString(R.string.not_null));
				port_EditText.requestFocus();
			} 
//			else if (!IPUtil.isIp(ip)) {
//				ip_EditText.setError(getString(R.string.error_invalid_ip));
//				ip_EditText.requestFocus();
//			} 
			else {
				AndroidUtil.setServerIPConfig(ip);
				AndroidUtil.setServerPortConfig(port);
				ip_EditText.setError(getString(R.string.success));
				ip_EditText.requestFocus();
				Toast.makeText(getApplicationContext(), R.string.ok,
						Toast.LENGTH_SHORT).show();
				GlobalVariable.getGlobalInstance().updateIp();
				GlobalVariable.getGlobalInstance().exit();
				startActivity(GlobalVariable.getGlobalInstance().intent
						.setAction(ActivityActionName.LOGIN));
			}
			break;

		default:
			break;
		}
	}

}
